<?
include "otamu_config.php";

define("_VIEWPATH_","views/export/");
if ($extype=="printer") $extension = "printer";
else $extension = "csv";

if (!isset($the_date)) $the_date = Date('d-m-Y');
if ( !isset($year) || (''==$year) ) $year = Date('Y');
if ( !isset($month) || (''==$month) ) $month = Date('m');
if ( !isset($day) || (''==$day) ) $day = Date('d');

if (!isset($_REQUEST['act'])) $_REQUEST['act'] = ''; 
$act = $_REQUEST['act'];

switch ($act) {

	case "outing_by_hostel_monthly" :
		if (!isset($output) || ($output=='')) $output = 'listing';

		if (!isset($_REQUEST['hostel_block_ids'])) {
			$and_block = "";
		}
		else {
			$block_arr = array();
			foreach ($_REQUEST['hostel_block_ids'] as $b) {
				$block_arr[] = "hostel_block_id=$b";
			}
			$and_block = " AND " . join(" OR ",$block_arr);
		}	

		$sql = "SELECT * FROM vw_outing WHERE bulan='$month' $and_block ORDER BY hostel_block_id ASC,out_time DESC";
		$students = pg_query($sql);
		include _VIEWPATH_."$act.$extension.php";
		break;

	case "outing_by_hostel_yearly" :
		if (!isset($output) || ($output=='')) $output = 'listing';

		if (!isset($_REQUEST['hostel_block_ids'])) {
			$and_block = "";
		}
		else {
			$block_arr = array();
			foreach ($_REQUEST['hostel_block_ids'] as $b) {
				$block_arr[] = "hostel_block_id=$b";
			}
			$and_block = " AND " . join(" OR ",$block_arr);
		}	

		$sql = "SELECT * FROM vw_outing WHERE tahun='$year' $and_block ORDER BY hostel_block_id ASC,out_time DESC";
		$students = pg_query($sql);
		include _VIEWPATH_."$act.$extension.php";
		break;

	case "outing_by_hostel_daily" :
		if (!isset($output) || ($output=='')) $output = 'listing';
		$date_to_find = dmy_date($the_date);

		if (!isset($_REQUEST['hostel_block_ids'])) {
			$and_block = "";
		}
		else {
			$block_arr = array();
			foreach ($_REQUEST['hostel_block_ids'] as $b) {
				$block_arr[] = "hostel_block_id=$b";
			}
			$and_block = " AND " . join(" OR ",$block_arr);
		}	
		
		$sql = "SELECT * FROM vw_outing WHERE tarikh_keluar='$date_to_find' $and_block ORDER BY hostel_block_id ASC,out_time DESC";
		$students = pg_query($sql);

		include _VIEWPATH_."$act.$extension.php";
		break;

	case "still_out" :
		if (!isset($_REQUEST['orderby']) || $_REQUEST['orderby']=='') {
			$orderby = " ORDER BY name ";
			if (!isset($_REQUEST['arrow']) || $_REQUEST['arrow']=='')
				$orderby .= "DESC";
			else
				$orderby .= $_REQUEST['arrow'];
		} else {
			$orderby = "ORDER BY " . $_REQUEST['orderby'] . " " . $_REQUEST['arrow'];
		}
		
		$sql = "SELECT vp.*,vlo.tarikh_out,vlo.jam_out,vlo.minit_out FROM vw_laporan_outing as vlo".
		       " INNER JOIN vw_pelajar as vp ON vp.nomatrik=vlo.nomatrik".
			   "  WHERE vlo.in_time ISNULL $orderby";
		$students = pg_query($sql);
		include _VIEWPATH_."$act.$extension.php";
		break;

	case "student_with_probs_ctg" :
		if (!isset($_REQUEST['orderby']) || $_REQUEST['orderby']=='') {
			$orderby = " ORDER BY crime_category_id ";
			if (!isset($_REQUEST['arrow']) || $_REQUEST['arrow']=='')
				$orderby .= "DESC";
			else
				$orderby .= $_REQUEST['arrow'];
		} else {
			$orderby = "ORDER BY " . $_REQUEST['orderby'] . " " . $_REQUEST['arrow'];
		}
		if (!isset($_REQUEST['ctg']) || $_REQUEST['ctg']=='') {
			$ctg = '1';
		}
		
		$sql = "SELECT vp.*,ob.id as outing_ban_id,ob.reason,ctg.description as crime_category_description FROM outing_bans as ob".
		       " INNER JOIN vw_pelajar as vp ON vp.nomatrik=ob.nomatrik".
			   " INNER JOIN crime_categories as ctg ON ctg.id=crime_category_id".
			   "  WHERE enforce='t' AND ctg.id = '$ctg' $orderby";
		$students = pg_query($sql);
		include _VIEWPATH_."$act.$extension.php";
		break;

	case "student_with_probs" :
		if (!isset($_REQUEST['orderby']) || $_REQUEST['orderby']=='') {
			$orderby = " ORDER BY name ";
			if (!isset($_REQUEST['arrow']) || $_REQUEST['arrow']=='')
				$orderby .= "DESC";
			else
				$orderby .= $_REQUEST['arrow'];
		} else {
			$orderby = "ORDER BY " . $_REQUEST['orderby'] . " " . $_REQUEST['arrow'];
		}
		
		$sql = "SELECT vp.*,ob.id as outing_ban_id,ob.reason,ctg.description as crime_category_description FROM outing_bans as ob".
		       " INNER JOIN vw_pelajar as vp ON vp.nomatrik=ob.nomatrik".
			   " INNER JOIN crime_categories as ctg ON ctg.id=crime_category_id".
			   "  WHERE enforce='t' $orderby";
		$students = pg_query($sql);
		include _VIEWPATH_."$act.$extension.php";
		break;

	case "list_rfid_reg":
		$sql = "SELECT * FROM vw_pelajar WHERE em_cardno NOTNULL ORDER BY name ASC";		
		$students = pg_query($sql);
		include _VIEWPATH_."$act.$extension.php";
		break;

	case "stat_outing_daily" :
		if (!isset($output) || ($output=='')) $output = 'listing';
		$date_to_find = dmy_date($the_date);
		$sql = "SELECT * FROM vw_outing WHERE tarikh_keluar='$date_to_find'";
		$students = pg_query($sql);
		break;

	case "stat_outing_monthly" :
		if (!isset($output) || ($output=='')) $output = 'listing';
		$year_to_find = $year;
		$month_to_find = $month;
		$sql = "SELECT * FROM vw_outing WHERE bulan='$month_to_find' AND tahun='$year_to_find'";
		$students = pg_query($sql);
		break;

	case "stat_outing_yearly" :
		if (!isset($output) || ($output=='')) $output = 'listing';
		$year_to_find = $year;
		$month_to_find = $month;
		$sql = "SELECT * FROM vw_outing WHERE bulan='$month_to_find' AND tahun='$year_to_find'";
		$students = pg_query($sql);
		break;

	case "outing_daily" :
		if (!isset($output) || ($output=='')) $output = 'listing';
		$date_to_find = dmy_date($the_date);
		$sql = "SELECT * FROM vw_outing WHERE tarikh_keluar='$date_to_find'";
		$students = pg_query($sql);
		include _VIEWPATH_."$act.$extension.php";
		break;

	case "outing_monthly" :
		if (!isset($output) || ($output=='')) $output = 'listing';
		$sql = "SELECT * FROM vw_outing WHERE bulan='$month' ORDER BY out_time DESC";
		$students = pg_query($sql);
		include _VIEWPATH_."$act.$extension.php";
		break;

	case "outing_yearly" :
		if (!isset($output) || ($output=='')) $output = 'listing';
		$sql = "SELECT * FROM vw_outing WHERE tahun='$year' ORDER BY out_time DESC";
		$students = pg_query($sql);
		include _VIEWPATH_."$act.$extension.php";
		break;
	/////////EOPMIS

	case "staff_daily_report" :
		if (!isset($year)) $year = Date('Y');
		if (!isset($month)) $month = Date('m');
		
		$_month_names = array('','Jan','Feb','Mac','Apr','Mei','Jun','Jul','Ogos','Sept','Okt','Nov','Dis');
		for ($i=1;$i<=12;$i++) {
			$k = $_month_names[$i];
			$_months["$k"] = $i;
		}
		
		$_selection_days = array();
		for ($i=1;$i<32;$i++) {
			$_selection_days["$i"] = $i;
		}
		$_range_days = array();
		
		if (!isset($day_only)) $day_only = ''; 
		if (!isset($day_start)) $day_start = ''; 
		if (!isset($day_end)) $day_end = ''; 
		if (!isset($day_option)) {
			$day_option = 1;
			$day_only = Date('d'); 
			if ($day_only < 10) $day_only = ereg_replace("0","",$day_only);
		}
		if ($day_option==1) {
			if ($day_only=='alldays') $_range_days = makeIntegerHash(1,31);
			else $_range_days[] = $day_only;
		}
		if ($day_option==2) {
			if ($day_start > $day_end) {
				$tmp = $day_start;
				$day_start = $day_end;
				$day_end = $tmp;
			}
			$_range_days = makeIntegerHash($day_start,$day_end);
		}

		$_objects = getHashedObject('staff_name','staff_ic_new','vw_lawatan_flat');

		include _VIEWPATH_."$act.$extension.php";
		break;
		
	case "staff_monthly_report" :
		if (!isset($year)) $year = Date('Y');
		$_month_names = array('','Jan','Feb','Mac','Apr','Mei','Jun','Jul','Ogos','Sept','Okt','Nov','Dis');
		$_range_months = array();
		for ($i=1;$i<=12;$i++) {
			$k = $_month_names[$i];
			$_months["$k"] = $i;
		}

		if (!isset($month_only)) $month_only = ''; 
		if (!isset($month_start)) $month_start = ''; 
		if (!isset($month_end)) $month_end = ''; 
		if (!isset($month_option)) {
			$month_option = 1;
			$month_only = Date('m'); $month_only = ereg_replace("0","",$month_only);
			$_range_months[] = $_month_names[$month_only];
		}
		if ($month_option==1) {
			if ($month_only=='allmonths') {
				$_range_months = $_month_names;
				array_shift($_range_months);
			}
			else $_range_months[] = $_month_names[$month_only];
		}
		if ($month_option==2) {
			if ($month_start > $month_end) {
				$tmp = $month_start;
				$month_start = $month_end;
				$month_end = $tmp;
			}
			for ($i=$month_start;$i<=$month_end;$i++) {
				$_range_months[] = $_month_names[$i];
			}
		}
		$_objects = getHashedObject('staff_name','staff_ic_new','vw_lawatan_flat');

		include _VIEWPATH_."$act.$extension.php";
		break;
		
	case "staff_yearly_report" :
		if (!isset($year_start)) $year_start = ''; 
		if (!isset($year_end)) $year_end = ''; 
		if (!isset($year_option)) {
			$year_option = 1;
			$year_only = Date('Y');
		}
		$_objects = getHashedObject('staff_name','staff_ic_new','vw_lawatan_flat');
		if ($year_option==1) {
			if ($year_only == 'allyears')
				$_years = getList('tahun','vw_lawatan_flat');
			else {
				$_years = array();
				$_years[] = $year_only;
			}
		}
		else {
			if ($year_end < $year_start) $_years = makeIntegerHash($year_end,$year_start);
			else $_years = makeIntegerHash($year_start,$year_end);
		}

		include _VIEWPATH_."$act.$extension.php";
		break;

	case "visitor_daily_report" :
		if (!isset($output) || ($output=='')) $output = 'listing';

		/* input : $the_date=dd-mm-YYYY
		           $order_by=fieldname
		*/
		if (!isset($year) || ($year=='')) $year = Date('Y');
		if (!isset($month) || ($month=='')) $month = Date('Y');
		if (!isset($the_date) || ($the_date=='')) {
			$year = Date('Y');
			$month = Date('m');
			$day = Date('d');
		}
		else {
			$arr = split("-",$the_date);
			$day   = $arr[0];
			$month = $arr[1];
			$year  = $arr[2];
		}

		if (!isset($order_by) || $order_by=='') $order_by = 'time_in';
		$sql = "SELECT * from vw_lawatan WHERE tahun='$year' AND bulan='$month' AND hari='$day' ORDER BY $order_by DESC";
		$res = pg_query($sql);
		$num = pg_num_rows($res);

		include _VIEWPATH_."$act.$extension.php";
		break;

	case "visitor_monthly_report" :
		if (!isset($year) || ($year=='')) $year = Date('Y');
		if (!isset($month) || ($month=='')) $month = Date('m');

		if (!isset($order_by) || $order_by=='') $order_by = 'time_in';
		$sql = "SELECT * from vw_lawatan WHERE tahun='$year' AND bulan='$month' ORDER BY $order_by DESC";
		$res = pg_query($sql);
		$num = pg_num_rows($res);
		include _VIEWPATH_."$act.$extension.php";
		break;

	case "visitor_yearly_report" :
		if (!isset($order_by) || $order_by=='') $order_by = 'time_in';
		if (!isset($year)) $year = Date('Y');
		$sql = "SELECT * FROM vw_lawatan WHERE tahun='$year' ORDER BY $order_by DESC";
		$res = pg_query($sql);
		$num = pg_num_rows($res);
		include _VIEWPATH_."$act.$extension.php";
		break;

	case "visitor_daily_statistic" :
		$the_dates  = array();
		$the_values = array();

		if ( !isset($year) || (''==$year) ) $year = Date('Y');
		if ( !isset($month) || (''==$month) ) $month = Date('m');
		$month = sprintf("%d",$month);

		for ($day=1;$day<=31;$day++) {
			$day = sprintf("%02d",$day);
			array_push($the_dates,$day);
			array_push($the_values,get_pelawat_monthly($year,$month,$day));
		}
		include _VIEWPATH_."$act.$extension.php";
		break;

	case "visitor_monthly_statistic" :
		$the_months = array();
		$the_values = array();

		//$a =  $gDateLocale-> GetShortMonth();

		if ( !isset($year) || (''==$year) ) $year = Date('Y');

		for ($month=1;$month<=12;$month++) {
			array_push($the_months,"$_month_lgnames[$month]");
			array_push($the_values,get_pelawat_yearly($year,$month));
		}
		include _VIEWPATH_."$act.$extension.php";
		break;

	case "visitor_yearly_statistic" :
		$the_years = array();
		$the_values = array();

		$sql = "SELECT tahun,count(id_pelawat) FROM vw_lawatan_flat group by tahun";
		$res = pg_query($sql);
		$num = pg_num_rows($res);
		for ($i=0;$i<$num;$i++) {
			$dat = pg_fetch_array($res,$i);
			$the_years[]  = $dat['tahun'];
			$the_values[] = $dat['count'];
		}
		include _VIEWPATH_."$act.$extension.php";
		break;

	default :
		//$res = pg_query("SELECT si.*,sg.description  FROM staff_infos as si INNER JOIN staff_groups as sg ON sg.id=si.staff_group_id ORDER BY name");
		include _VIEWPATH_."summary.php";
		
}

?>